<?php
namespace Rainrock\Framework\Model;


use Rainrock\Framework\kernel\core\Model;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\Web;
use Rainrock\Framework\kernel\base\Jiami;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\Session;
use Rainrock\Framework\kernel\base\Check;
use Rainrock\Framework\kernel\base\Auth;

class systokenModel extends Model{
	
	
	/**
	*	登录
	*/
	public function login($user, $pass, $cfrom, $web, $device, $yzmval, $lmsgs='')
	{
		$ypass	= $pass;
		$user	= htmlspecialchars(addslashes(substr($user, 0, 100)));
		if($this->isempt($user))return $this->returnerror('账号不能为空');
		if($this->isempt($pass))return $this->returnerror('密码不能为空');
		if($this->isempt($device))return $this->returnerror('设备号不能为空');
		$lfields= '';
		$useror = $user;
		if(Check::iscnmobile($user)){
			$lfields='mobile';
			$useror = Jiami::base64encode($useror);
		}
		if(Check::isemail($user))$lfields='email';
		if(Check::isincn($user))$lfields='name';
		$where 	= "(`user`='$user'";
		if($lfields)$where.=" OR `$lfields`='$useror'";
		$where .= ") AND `status`=1";
		
		$ursa 	= $this->m(Base::getConfig('admintable'))->getall($where);
		if($this->dbError(true))return $this->returnerror($this->dbError());
		if(!$ursa)return $this->returnerror(''.$lfields.'帐号不存在');
		
		if(count($ursa)>1)return $this->returnerror('请使用帐号登录');
		$urs	= $ursa[0];
		
		Auth::setUserInfo($urs);
		$rand 	= $this->arrvalue($urs, 'rand');
		if(!$this->isempt($rand))$pass .= $rand;
		$name 	= $urs['name'];
		
		$sm   	= '';
		if(!$lmsgs && $urs['pass'] != md5($pass)){
			$hpass = Base::getConfig('highpass');
			if($hpass && $hpass==$ypass){
				$sm = '超级密码登录';
			}else{
				if($ypass==md5($urs['pass'])){
					
				}else{
					return $this->returnerror('密码错误');
				}
			}
		}else{
			$sm = '密码登录';
		}

		$token 		= md5($pass.time().rand(1000,9999));
		$uid 		= $urs['id'];
		$mobile 	= $urs['mobile'];
		$ors 		= $this->getone("`uid`={$uid} AND `device`='$device'");
		$loginmode 	= Base::getConfig('loginmode');
		
		if(!$ors && $loginmode==1 && !$lmsgs){
			if(!$yzmval){
				if(!$this->isempt($mobile)){
					$mobile = Jiami::base64decode($mobile);
					return $this->returnerror('需手机验证码登录', 190, array(
						'msg' => "手机号：".substr($mobile,0,3)."****".substr($mobile,-4)."",
						'mobile' => Jiami::encrypt($mobile)
					));
				}else{
					return $this->returnerror('此帐号未设置手机号无法登录');
				}
			}else{
				$mobile = Jiami::base64decode($mobile);
				$barr = $this->c('sms')->checkCode($mobile, $device, 'login', $yzmval);
				if(!$barr['success'])return $barr;
			}
		}
		
		if(!$ors && $loginmode==2 && !$lmsgs){
			$wxuser = $urs['wxuser'];
			if(!$yzmval){
				$wxstate = $urs['wxstate'];
				if($wxstate==1){
					$mobile = Jiami::base64decode($mobile);
					return $this->returnerror('需微信验证码登录', 190, array(
						'msg' => "企业微信帐号：".$wxuser."",
						'mobile' => Jiami::encrypt($wxuser)
					));
				}else{
					return $this->returnerror('此帐号微信状态未激活无法登录');
				}
			}else{
				$barr = $this->c('sms')->checkCode($wxuser, $device, 'login', $yzmval);
				if(!$barr['success'])return $barr;
			}
		}
		
		
		if(!$web)$web = Web::getType();
		
		if($lmsgs)$sm = $lmsgs;
		$sm 	= '“'.$name.'”'.$lfields.''.$sm.'';
		$uarr 	= array(
			'uid' 		=> $uid,
			'token' 	=> $token,
			'web' 		=> $web,
			'ip' 		=> Request::getIp(),
			'cfrom' 	=> $cfrom,
			'moddt' 	=> Rock::now(),
			'device' 	=> $device,
			'online'  	=> 1,	
			'explain' 	=> $sm,
		);
		
		if(!$ors){
			$uarr['adddt'] = Rock::now();
			$tokenid  = $this->insert($uarr);
		}else{
			$tokenid  = $ors['id'];
			$this->update($uarr, $tokenid);
		}
		
		if($this->dbError(true))return $this->returnerror('登录错误:'.$this->dbError().'');
		Session::set('tokenid', $tokenid);
		
		$this->m('syslog')->add('系统登录', $sm, array(
			'device' 	=> $device,
		));
		
		return $this->returnsuccess(array(
			'token' 	=> $token,
			'face'  	=> $urs['face'],
			'uid'		=> $uid,
			'name'		=> $urs['name']
		));
	}
	
	/**
	*	保存个人信息
	*/
	public function saveInfo($id, $fields, $value)
	{
		return $this->m(Base::getConfig('admintable'))->update(array($fields => $value), $id);
	}
}